package com.why.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.servicebase.exception.GuilException;
import com.why.serviceedu.bean.EduChapter;
import com.why.serviceedu.bean.EduVideo;
import com.why.serviceedu.bean.chapter.ChapterVo;
import com.why.serviceedu.bean.chapter.VideoVo;
import com.why.serviceedu.mapper.EduChapterMapper;
import com.why.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.serviceedu.service.EduVideoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-02-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    EduVideoService service;


    @Override
    public List<ChapterVo> getAllChapterVideoByCourseId(String courseId) {

        //1.先获取课程所对应的章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterQueryWrapper);
        //2.再获取课程所对应的小节
        QueryWrapper<EduVideo> videoQueryWrapper= new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = service.list(videoQueryWrapper);
        ArrayList<ChapterVo> result = new ArrayList<>();
        //遍历章节信息
        for(EduChapter chapter:eduChapterList){
            ChapterVo chapterVo = new ChapterVo();
            //设置单个章节信息
            chapterVo.setId(chapter.getId());
            chapterVo.setTitle(chapter.getTitle());
            //根据章节id遍历小节列表
            ArrayList<VideoVo> VideoVoList = new ArrayList<>();
            for(EduVideo video:eduVideoList){

                if(video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(video.getId());
                    videoVo.setTitle(video.getTitle());
                    videoVo.setVideoSourceId(video.getVideoSourceId());
                    VideoVoList.add(videoVo);
                }
            }
            chapterVo.setVideoVoList(VideoVoList);
            result.add(chapterVo);

        }


        return result;
    }

    @Override
    public boolean deleteById(String chapterId) {
        //baseMapper.deleteById(chapterId);
       //查询小节表，要是有数据就不删除，否则就删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = service.count(wrapper);
        if(count!=0) {
            throw new GuilException(20001, "请先删除小节再删除章节！");
        }else {
             count = baseMapper.deleteById(chapterId);
        }
        return count>0;
    }

    /**
     * 删除课程对应的章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int delete = baseMapper.delete(wrapper);
        if(delete<=0){
            throw new GuilException(20001,"章节删除失败！");
        }

    }


}
